
/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) xio.name 2006</p>
 * @author xio
 */
function SaveActionListener(xiorkFlow) {
    this.xiorkFlow = xiorkFlow;
}

SaveActionListener.prototype.actionPerformed = function (obj) {
    var wrapper = this.xiorkFlow.getWrapper();
    var toolbar = this.xiorkFlow.getToolBar();
    var name = wrapper.getModel().getName();
	if(this.xiorkFlow.xiorkFlowWrapper.metaNodes.size()<1){
		alertMessage("工作流不能为空。");
		return;
	}
	var duplicateNames = findDuplicateNames(wrapper.getModel());
	if (duplicateNames != ""){
		alertMessage("不能保存工作流。原因是有重复的节点名字： "+duplicateNames
		   +". \n\n请改正名字然后保存。");
    return;
	}
    if (!name) {
        saveProcessConsiderDupleName(this.xiorkFlow, false);
    } else {
		saveProcessIgnoreDupleName(this.xiorkFlow, name);
	}
};

function saveProcessConsiderDupleName(xiorkFlow, doClose)
{
	var name = prompt("输入工作流名称", "");
    if (name != null && name != "") {
/*		for(var i=0; i<processDefinitionList.length; i++){
			if(name == processDefinitionList[i].firstChild.nodeValue){
				ShowWorkFlowSaveDialog(xiorkFlow, name,	doClose);
				return;
			}
		}*/
		saveProcessIgnoreDupleName(xiorkFlow, name, doClose);
    }
}

function saveProcessIgnoreDupleName(xiorkFlow, name, doClose)
{
	var addProcess = new AddProcess(xiorkFlow.getWrapper(), xiorkFlow.getToolBar(),
			xiorkFlow.getProcessList());
    addProcess.addProcess(name);
    if(doClose){
		exitwindow(); 
    }
    
}

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) xio.name 2006</p>
 * @author xio
 */
 
function SaveAsActionListener(xiorkFlow) {
    this.xiorkFlow = xiorkFlow;
}

SaveAsActionListener.prototype.actionPerformed = function (obj) {
    var wrapper = this.xiorkFlow.getWrapper();
    var toolbar = this.xiorkFlow.getToolBar();
    var name = wrapper.getModel().getName();
	if(this.xiorkFlow.xiorkFlowWrapper.metaNodes.size()<1){
		alertMessage("工作流不能为空。");
		return;
	}
   var duplicateNames = findDuplicateNames(wrapper.getModel());
   if (duplicateNames != ""){
     alertMessage("不能保存工作流。原因是有重复的节点名字："+duplicateNames
        +". \n\n请改正名字然后保存。");
     return;
   }
    
    
    var name = wrapper.getModel().getName();
  //  if (!name) {
        /*name = prompt("Please enter workflow name", "");
        if (name != null && name != "") {
            var addProcess = new AddProcess(wrapper, toolbar, this.xiorkFlow.getProcessList());
            addProcess.addProcess(name);
       }*/
       saveProcessConsiderDupleName(this.xiorkFlow, false);
  //   }else { 
    //    var addProcess = new AddProcess(wrapper, toolbar, this.xiorkFlow.getProcessList());
    //    addProcess.addProcess(name);        
  // }
 
};

function findDuplicateNames(xiorkFlowModel)
{
	var names = {};
  var duplicateNames = "";
  var metaNodeModels = xiorkFlowModel.getMetaNodeModels();
  
  for (var i = 0; i < metaNodeModels.size(); i++) {
  	var name = metaNodeModels[i].getText();
  	if (names[name] == null) {
  		names[name] = name;
  	}        
  	else {
  		if (duplicateNames != "")
  		  duplicateNames += ", ";
  		duplicateNames += name;
  	}
  }
  
  return duplicateNames;
}


/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) mark.name 2006</p>
 * @author mark
 */
function CloseActionListener(xiorkFlow) {
    this.xiorkFlow = xiorkFlow;
}

CloseActionListener.prototype.actionPerformed = function (obj) {
    //var wrapper = this.xiorkFlow.getWrapper(); 
    //if(this.xiorkFlow.getWrapper().isChanged()){
    //  ShowWorkFlowCloseDialog(this.xiorkFlow);
    //}
    //else
    //{ 
        exitwindow();   
    //} 
};

function IsChangeWorkFlow(xiorkFlowModel){
    return true;
}
